//
//  MyViewController.m
//  UIA段项目 墨家天气
//
//  Created by dllo on 15/8/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MyViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AddCity.h"
#import "MyCollectViewController.h"
#import "RemoveCache.h"
#import "AppDelegate.h"
//#import "MyCollectCell.h"
#import "MyCell.h"
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)UIView *headerView;
@property (nonatomic, retain) UIView *dark;
@property (nonatomic,retain)UITableView *tableV;
@property (nonatomic,retain)UISwitch *swit;
@property (nonatomic,retain)UILabel *f;
@end

@implementation MyViewController
-(void)dealloc
{
    
    
    [_f release];
    [_swit release];
    [_tableV release];
    [_headerView release];
    [_dark release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTableView];
    self.automaticallyAdjustsScrollViewInsets = NO;

 
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       self.swit = [[UISwitch alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 60 * OffWidth, 10 * OffHeight, 50 * OffWidth, 40  *OffHeight)];
       self.f = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 60 * OffWidth, 5 * OffHeight, 50 * OffWidth, 40 * OffHeight)];
       self.f.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
    }
    return self;
}

- (void)addTableView
{
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    _tableV.delegate = self;
    _tableV.dataSource = self;
    _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(0, - 100 * OffHeight, self.view.frame.size.width, 300 * OffHeight)];
    view.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
    _tableV.tableHeaderView = view;
    
    UIButton *join = [UIButton buttonWithType:UIButtonTypeSystem];
    join.frame = CGRectMake((view.frame.size.width - 100  * OffWidth)/ 2,(view.frame.size.height - 100 * OffHeight) / 2, 100 * OffWidth, 100 * OffHeight);
    join.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.5];
    join.layer.cornerRadius = 50 * OffWidth;
    join.layer.masksToBounds = YES;
    [join setBackgroundImage:[UIImage imageNamed:@"tou.jpg"] forState:UIControlStateNormal];
    [join addTarget:self action:@selector(join:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:join];
    
    
    
   
    
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    imageV.image = [UIImage imageNamed:@"888.jpg"];
    [_tableV setBackgroundView:imageV];
    
    
    UIBlurEffect *ef = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effV = [[UIVisualEffectView alloc]initWithEffect:ef];
    effV.frame = [UIScreen mainScreen].bounds;
    [imageV addSubview:effV];
    

    [self.view addSubview:_tableV];
    [_tableV release];
    [view release];
}
- (void)join:(UIButton *)button
{
    NSLog(@"1");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    MyCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"e"];
    if (cell == nil) {
        cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"e"];
      }
   
    
    NSArray *arrTitle = [NSArray arrayWithObjects:@"清理缓存",@"城市管理",@"夜间模式",@"我的收藏", nil];
    NSArray *arrImage = [NSArray arrayWithObjects:[UIImage imageNamed:@"xiaohuojian.png"],[UIImage imageNamed:@"shezhi.png"],[UIImage imageNamed:@"yueliang"],[UIImage imageNamed:@"shou.png"] ,nil];
    cell.titleStr.text = [arrTitle objectAtIndex:indexPath.row];
    cell.imamgeV.image = [arrImage objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    
    if (indexPath.row == 0) {
        
        NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *myPath = [array firstObject];
        RemoveCache *remove = [[RemoveCache alloc]init];
        CGFloat a = [remove floatWithPath:myPath];
        NSString *aa = [NSString stringWithFormat:@"%0.2fM",a];
        
       
        self.f.text =  aa ;
        self.f.textColor = [UIColor whiteColor];
        self.f.layer.masksToBounds = YES;
        self.f.textAlignment = 1;
        self.f.font = [UIFont boldSystemFontOfSize:13 * OffWidth];
        self.f.layer.cornerRadius = 20 * OffWidth;
        
        [cell addSubview:_f];
        [remove release];
        
        
    }
    if (indexPath.row == 2) {
        
        self.swit.backgroundColor = [UIColor clearColor];
        [_swit addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:_swit];
        
        self.dark = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        self.dark.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        _dark.userInteractionEnabled = NO;
        [_swit release];
        
    }

    return cell;
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 4;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        
        [self clean];
        
    }else if(indexPath.row == 1){
        
        AddCity *add = [[AddCity alloc]init];
        NSURL *url = [NSURL URLWithString:@"http://b.appsimg.com/2015/09/08/666/1441685787_7415.jpg"];
        add.backUrl  =url;
        [self.navigationController pushViewController:add animated:YES];;
        
    }else if (indexPath.row == 2){
        
        
        
        
    }else{
        
        MyCollectViewController  *collect = [[MyCollectViewController alloc]init];
        
        
        [self presentViewController:collect animated:YES completion:nil];
        
        
    }
    
    
}


- (void)clean
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    
    UIAlertAction *clean = [UIAlertAction actionWithTitle:@"清除缓存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        RemoveCache *remove = [[RemoveCache alloc]init];
        [remove removeCache];
        [remove release];
        
        self.f.text = @"0.00M";
        self.f.backgroundColor = [[UIColor greenColor]colorWithAlphaComponent:0.7];
        

    
    }];
    
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        
    }];
    
    [alert addAction:clean];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
    
                            
}
- (void)switchAction:(UISwitch *)swit
{
    if (swit.on == YES) {
        
        AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        [app.window addSubview:_dark];
    } else {
        [_dark removeFromSuperview];
    }
}

@end
